package gui;

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.io.File;
import java.security.Key;
import java.util.HashMap;
import java.util.HashSet;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JPasswordField;

import code.KeyStoreManager;
import code.Language;
import code.LogEncryption;
import code.Settings;
import code.SymmetricKeyMaker;

public class KeyCreationSymmetric extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JTextField jTextField2 = null;
	private JButton jButton = null;
	private JComboBox jComboBox = null;

	ButtonGroup rbgroup = new ButtonGroup();
	private JRadioButton jRadioButton = null;
	private JRadioButton jRadioButton1 = null;
	private JRadioButton jRadioButton2 = null;
	private JPasswordField jPasswordField = null;
	private JLabel jLabel5 = null;
	private JPasswordField jPasswordField1 = null;
	private String selected_algorithm;
	private HashMap hm= new HashMap();
	private int selected_size;
	ATOCOmain atocoframe;
	private HashMap labels;
	private JButton jButton1 = null;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JLabel jLabel10 = null;
	JPanel prevclass;
	private KeyStoreManager ksm;
	String prevclassvalue;
	private Settings s;
	Language l = new Language();

	public KeyCreationSymmetric(ATOCOmain atocoframe, String selected_algorithm) {
		super();
		s= new Settings();
		labels = l.changeLanguage((String) s.global.get("language"), "KeyCreationSymmetric", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		this.selected_algorithm = selected_algorithm;
		initialize();
	}


	public KeyCreationSymmetric(ATOCOmain atocoframe, String selected_algorithm, JPanel prevclass, String prevclassvalue) {
		super();
		labels = l.changeLanguage((String) s.global.get("language"), "KeyCreationSymmetric", (String) s.global.get("installPath"));
		System.out.println("Labels: "+labels);
		this.atocoframe=atocoframe;
		this.selected_algorithm = selected_algorithm;
		this.prevclass=prevclass;
		this.prevclassvalue=prevclassvalue;
		initialize();
	}




	private void initialize() {
		hm.put("AES", 0);//128 192 256
		hm.put("DES", 1);//56
		hm.put("TripleDES", 2);//112 168
		hm.put("RC2", 3);//
		hm.put("RC4", 4);
		hm.put("RC5", 5);
		hm.put("RC6", 6);
		hm.put("Blowfish", 7);
		//hm.put("Rjindael", 8);
		hm.put("Serpent", 8);


		/*jLabel10 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel10.setBounds(new Rectangle(390, 220, 25, 25));*/
		jLabel9 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel9.setBounds(new Rectangle(390, 180, 25, 25));
		jLabel8 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel8.setBounds(new Rectangle(390, 140, 25, 25));
		jLabel7 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel7.setBounds(new Rectangle(390, 100, 25, 25));
		jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel6.setBounds(new Rectangle(390, 60, 25, 25));
		/*jLabel5 = new JLabel();
		jLabel5.setBounds(new Rectangle(30, 220, 140, 25));
		jLabel5.setText("Password ( Re-enter ) :");*/
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(30, 180, 120, 25));
		jLabel4.setText((String)labels.get("jLabel4"));
		
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(30, 140, 120, 28));
		jLabel3.setText((String)labels.get("jLabel3"));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(30, 100, 120, 25));
		jLabel2.setText((String)labels.get("jLabel2"));
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(30, 60, 120, 25));
		jLabel1.setText((String)labels.get("jLabel1"));
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(30, 20, 335, 25));
		jLabel.setText((String)labels.get("jLabel"));
		this.setSize(749, 404);
		this.setLayout(null);
		this.add(jLabel, null);
		this.add(jLabel1, null);
		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.add(jLabel4, null);
		this.add(getJTextField2(), null);
		this.add(getJButton(), null);
		this.setBackground(Color.WHITE);
		this.add(getJComboBox(), null);
		this.add(getJRadioButton(), null);
		this.add(getJRadioButton1(), null);
		this.add(getJRadioButton2(), null);
		setRadioButtons();
		jRadioButton.setSelected(true);
		if(!jRadioButton.getText().equals("N/A")){
			selected_size=Integer.parseInt(jRadioButton.getText());
		}
		this.add(getJPasswordField(), null);
		//this.add(jLabel5, null);
		//this.add(getJPasswordField1(), null);
		this.add(getJButton1(), null);
		this.add(jLabel6, null);
		this.add(jLabel7, null);
		this.add(jLabel8, null);
		this.add(jLabel9, null);
		//this.add(jLabel10, null);
	}

	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(200, 140, 150, 25));
		}
		return jTextField2;
	}


	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(270, 270, 80, 30));
			jButton.setText((String)labels.get("jButton"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					String selected= (String)jComboBox.getSelectedItem();
					SymmetricKeyMaker key;
					if(!jTextField2.getText().equals("")&&!jPasswordField.getText().equals("")){
					if(selected.equalsIgnoreCase("AES")||selected.equalsIgnoreCase("TripleDES")||selected.equalsIgnoreCase("DES"))
					{
						System.out.println("\n\n\n\n\n\n\n\n"+selected_size+"\n\n\n\n\n\n");
						key= new SymmetricKeyMaker(selected, selected_size);
					}
					else{
						key= new SymmetricKeyMaker(selected);
					}
					key.generateKey();

					ksm = new KeyStoreManager(
							(String)Settings.global.get("personalKeyStoreFile"), 
							(String)Settings.global.get("personalKeyStorePassword"));
					if (ksm.getKeyStore() == null) {
						System.out.println("Keystore file not found. Creating one.");
						ksm.createKeyStore(
								(String)Settings.global.get("personalKeyStoreFile"), 
								(String)Settings.global.get("personalKeyStorePassword"));
					}
					ksm.saveSecretKey(jTextField2.getText(), key.getKey(), jPasswordField.getText());
					ksm.saveKeyStore();
					LogEncryption log=new LogEncryption();
					log.initiateKeylist();
					log.addtoKeylist(jTextField2.getText(), selected);


					System.out.println(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
					setprevclasskey();
					}


				}
			});
		}
		return jButton;
	}

	private void setprevclasskey(){
		if(prevclassvalue.equalsIgnoreCase("AES/CBC")){
			AESEncryptionCBCMode aescbc=(AESEncryptionCBCMode)prevclass;
			aescbc.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(aescbc);
		}else if(prevclassvalue.equalsIgnoreCase("AES/OFB")){
			AESEncryptionOFBMode aesofb=(AESEncryptionOFBMode)prevclass;
			aesofb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(aesofb);
		}else if(prevclassvalue.equalsIgnoreCase("AES/CFB")){
			AESEncryptionCFBMode aescfb=(AESEncryptionCFBMode)prevclass;
			aescfb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(aescfb);
		}else if(prevclassvalue.equalsIgnoreCase("AES/ECB")){
			AESEncryptionECBMode aesecb=(AESEncryptionECBMode)prevclass;
			aesecb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(aesecb);
		}else if(prevclassvalue.equalsIgnoreCase("DES/ECB")){
			DESEncryptionECBMode desecb=(DESEncryptionECBMode)prevclass;
			desecb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(desecb);
		}else if(prevclassvalue.equalsIgnoreCase("DES/CFB")){
			DESEncryptionCFBMode descfb=(DESEncryptionCFBMode)prevclass;
			descfb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(descfb);
		}else if(prevclassvalue.equalsIgnoreCase("DES/CBC")){
			DESEncryptionCBCMode descbc=(DESEncryptionCBCMode)prevclass;
			descbc.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(descbc);
		}else if(prevclassvalue.equalsIgnoreCase("DES/OFB")){
			DESEncryptionOFBMode desofb=(DESEncryptionOFBMode)prevclass;
			desofb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(desofb);
		}else if(prevclassvalue.equalsIgnoreCase("TripleDES/CBC")){
			TripleDESEncryptionCBCMode tdescbc=(TripleDESEncryptionCBCMode)prevclass;
			tdescbc.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(tdescbc);
		}else if(prevclassvalue.equalsIgnoreCase("TripleDES/ECB")){
			TripleDESEncryptionECBMode tdesecb=(TripleDESEncryptionECBMode)prevclass;
			tdesecb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(tdesecb);
		}else if(prevclassvalue.equalsIgnoreCase("TripleDES/CFB")){
			TripleDESEncryptionCFBMode tdescfb=(TripleDESEncryptionCFBMode)prevclass;
			tdescfb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(tdescfb);
		}else if(prevclassvalue.equalsIgnoreCase("TripleDES/OFB")){
			TripleDESEncryptionOFBMode tdesofb=(TripleDESEncryptionOFBMode)prevclass;
			tdesofb.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(tdesofb);
		}else if(prevclassvalue.equalsIgnoreCase("KeyCreation")){
			KeyCreation kc= (KeyCreation)prevclass;
			atocoframe.contentPane(kc);
		}else{
			OtherEncryptions otherenc= (OtherEncryptions)prevclass;
			otherenc.setKey(ksm.getSecretKey(jTextField2.getText(), jPasswordField.getText()));
			atocoframe.contentPane(otherenc);
		}
	}

	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(200, 60, 120, 25));
			jComboBox.setBackground(Color.WHITE);
			jComboBox.addItem("AES");
			jComboBox.addItem("DES");
			jComboBox.addItem("TripleDES");
			jComboBox.addItem("RC2");
			jComboBox.addItem("RC4");
			jComboBox.addItem("RC5");
			jComboBox.addItem("RC6");
			jComboBox.addItem("Blowfish");
			//jComboBox.addItem("Rjindael");
			jComboBox.addItem("Serpent");

			jComboBox.setSelectedIndex((Integer)hm.get(selected_algorithm));
			jComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					selected_algorithm=(String)jComboBox.getSelectedItem();
					setRadioButtons();
				}
			});

		}
		return jComboBox;
	}


	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setBounds(new Rectangle(200, 100, 50, 25));
			jRadioButton.setBackground(Color.WHITE);
			jRadioButton.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton.isSelected())
					{
						if(!jRadioButton.getText().equals("N/A")){
							selected_size=Integer.parseInt(jRadioButton.getText());
						}
					}

				}
			});
			rbgroup.add(jRadioButton);

		}
		return jRadioButton;
	}

	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(260, 100, 50, 25));
			jRadioButton1.setBackground(Color.WHITE);
			jRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton1.isSelected())
					{
						if(!jRadioButton.getText().equals("N/A")){
							selected_size=Integer.parseInt(jRadioButton1.getText());
						}
					}

				}
			});
			rbgroup.add(jRadioButton1);
		}
		return jRadioButton1;
	}


	private JRadioButton getJRadioButton2() {
		if (jRadioButton2 == null) {
			jRadioButton2 = new JRadioButton();
			jRadioButton2.setBounds(new Rectangle(320, 100, 50, 25));
			jRadioButton2.setBackground(Color.WHITE);
			jRadioButton2.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton2.isSelected())
					{
						if(!jRadioButton.getText().equals("N/A")){
						selected_size=Integer.parseInt(jRadioButton2.getText());
						}
					}

				}
			});
			rbgroup.add(jRadioButton2);
		}
		return jRadioButton2;
	}

	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(200, 180, 150, 25));
		}
		return jPasswordField;
	}


	/*private JPasswordField getJPasswordField1() {
		if (jPasswordField1 == null) {
			jPasswordField1 = new JPasswordField();
			jPasswordField1.setBounds(new Rectangle(200, 220, 150, 25));
		}
		return jPasswordField1;
	}*/

	private void setRadioButtons(){
		if(selected_algorithm.equalsIgnoreCase("AES")){
			jRadioButton.setText("128");
			jRadioButton1.setText("192");
			jRadioButton2.setText("256");
			jRadioButton.setVisible(true);
			jRadioButton1.setVisible(true);
			jRadioButton2.setVisible(true);
		}else if(selected_algorithm.equalsIgnoreCase("DES")){
			jRadioButton.setText("56");
			jRadioButton.setVisible(true);
			jRadioButton1.setVisible(false);
			jRadioButton2.setVisible(false);
		}else if(selected_algorithm.equalsIgnoreCase("TripleDES")){
			jRadioButton.setText("112");
			jRadioButton1.setText("168");
			jRadioButton.setVisible(true);
			jRadioButton1.setVisible(true);
			jRadioButton2.setVisible(false);
		}else{
			jRadioButton.setText("N/A");
			jRadioButton.setVisible(true);
			jRadioButton1.setVisible(false);
			jRadioButton2.setVisible(false);

		}
		jRadioButton.setSelected(true);
	}


	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(170, 270, 80, 30));
			jButton1.setText((String)labels.get("jButton1"));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					if(prevclassvalue.equalsIgnoreCase("AES/CBC")){
						AESEncryptionCBCMode aescbc=(AESEncryptionCBCMode)prevclass;

						atocoframe.contentPane(aescbc);
					}else if(prevclassvalue.equalsIgnoreCase("AES/OFB")){
						AESEncryptionOFBMode aesofb=(AESEncryptionOFBMode)prevclass;

						atocoframe.contentPane(aesofb);
					}else if(prevclassvalue.equalsIgnoreCase("AES/CFB")){
						AESEncryptionCFBMode aescfb=(AESEncryptionCFBMode)prevclass;

						atocoframe.contentPane(aescfb);
					}else if(prevclassvalue.equalsIgnoreCase("AES/ECB")){
						AESEncryptionECBMode aesecb=(AESEncryptionECBMode)prevclass;

						atocoframe.contentPane(aesecb);
					}else if(prevclassvalue.equalsIgnoreCase("DES/ECB")){
						DESEncryptionECBMode desecb=(DESEncryptionECBMode)prevclass;

						atocoframe.contentPane(desecb);
					}else if(prevclassvalue.equalsIgnoreCase("DES/CFB")){
						DESEncryptionCFBMode descfb=(DESEncryptionCFBMode)prevclass;

						atocoframe.contentPane(descfb);
					}else if(prevclassvalue.equalsIgnoreCase("DES/CBC")){
						DESEncryptionCBCMode descbc=(DESEncryptionCBCMode)prevclass;

						atocoframe.contentPane(descbc);
					}else if(prevclassvalue.equalsIgnoreCase("DES/OFB")){
						DESEncryptionOFBMode desofb=(DESEncryptionOFBMode)prevclass;

						atocoframe.contentPane(desofb);
					}else if(prevclassvalue.equalsIgnoreCase("TripleDES/CBC")){
						TripleDESEncryptionCBCMode tdescbc=(TripleDESEncryptionCBCMode)prevclass;

						atocoframe.contentPane(tdescbc);
					}else if(prevclassvalue.equalsIgnoreCase("TripleDES/ECB")){
						TripleDESEncryptionECBMode tdesecb=(TripleDESEncryptionECBMode)prevclass;

						atocoframe.contentPane(tdesecb);
					}else if(prevclassvalue.equalsIgnoreCase("TripleDES/CFB")){
						TripleDESEncryptionCFBMode tdescfb=(TripleDESEncryptionCFBMode)prevclass;

						atocoframe.contentPane(tdescfb);
					}else if(prevclassvalue.equalsIgnoreCase("TripleDES/OFB")){
						TripleDESEncryptionOFBMode tdesofb=(TripleDESEncryptionOFBMode)prevclass;

						atocoframe.contentPane(tdesofb);
					}else if(prevclassvalue.equalsIgnoreCase("KeyCreation")){
						KeyCreation kc= (KeyCreation)prevclass;
						atocoframe.contentPane(kc);
					}else{
						OtherEncryptions otherenc= (OtherEncryptions)prevclass;

						atocoframe.contentPane(otherenc);
					}
				}
			});
		}
		return jButton1;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
